<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set callback function for LOAD DATA LOCAL INFILE command</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.set-local-infile-default.html">mysqli::set_local_infile_default</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.sqlstate.html">mysqli::$sqlstate</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.set-local-infile-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysqli::set_local_infile_handler</h1>
  <h1 class="refname">mysqli_set_local_infile_handler</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">mysqli::set_local_infile_handler</span> -- <span class="refname">mysqli_set_local_infile_handler</span> &mdash; <span class="dc-title">Set callback function for LOAD DATA LOCAL INFILE command</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mysqli.set-local-infile-handler-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>mysqli::set_local_infile_handler</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.mysqli.html" class="type mysqli">mysqli</a></span> <code class="parameter">$link</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$read_func</code></span>
   )</div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>mysqli_set_local_infile_handler</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.mysqli.html" class="type mysqli">mysqli</a></span> <code class="parameter">$link</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$read_func</code></span>
   )</div>

  
  <p class="para rdfs-comment">Set callback function for LOAD DATA LOCAL INFILE command</p>

  <p class="para">
   The callbacks task is to read input from the file specified in the 
   <em>LOAD DATA LOCAL INFILE</em> and to reformat it into
   the format understood by <em>LOAD DATA INFILE</em>.
  </p>

  <p class="para">
   The returned data needs to match the format specified in the
   <em>LOAD DATA</em> 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysqli.set-local-infile-handler-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<em><code class="parameter">
link</code></em></dt>
<dd>
<p class="para">仅以过程化样式：由<span class="function"><a href="function.mysqli-connect.html" class="function">mysqli_connect()</a></span> 或 <span class="function"><a href="mysqli.init.html" class="function">mysqli_init()</a></span>
返回的链接标识。</p></dd>

    
     <dt>
<em><code class="parameter">read_func</code></em></dt>

     <dd>

      <p class="para">
       A callback function or object method taking the following parameters:
      </p>
      <dl>

       
        <dt>
<em><code class="parameter">stream</code></em></dt>

        <dd>
<p class="para">A PHP stream associated with the SQL commands INFILE</p></dd>

       
       
        <dt>
<em><code class="parameter">&amp;buffer</code></em></dt>

        <dd>
<p class="para">A string buffer to store the rewritten input into</p></dd>

       
       
        <dt>
<em><code class="parameter">buflen</code></em></dt>

        <dd>
<p class="para">The maximum number of characters to be stored in the buffer</p></dd>

       
       
        <dt>
<em><code class="parameter">&amp;errormsg</code></em></dt>

        <dd>
<p class="para">If an error occurs you can store an error message in here</p></dd>

       
      </dl>

     </dd>

    
   </dl>

  </p>
  <p class="para">
   The callback function should return the number of characters stored
   in the <em><code class="parameter">buffer</code></em> or a negative value if an error
   occurred.
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysqli.set-local-infile-handler-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mysqli.set-local-infile-handler-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1654">
   <p><strong>Example #1 <span class="methodname"><strong>mysqli::set_local_infile_handler()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_init</span><span style="color: #007700">();<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">real_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,</span><span style="color: #DD0000">"root"</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">callme</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buflen</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$errmsg</span><span style="color: #007700">)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buffer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;convert&nbsp;to&nbsp;upper&nbsp;case&nbsp;and&nbsp;replace&nbsp;","&nbsp;delimiter&nbsp;with&nbsp;[TAB]<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buffer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">","</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /><br /><br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Input:\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_local_infile_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">"callme"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"LOAD&nbsp;DATA&nbsp;LOCAL&nbsp;INFILE&nbsp;'input.txt'&nbsp;INTO&nbsp;TABLE&nbsp;t1"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_local_infile_default</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;t1"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\nResult:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_assoc</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">join</span><span style="color: #007700">(</span><span style="color: #DD0000">","</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_init</span><span style="color: #007700">();<br />&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_real_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,</span><span style="color: #DD0000">"root"</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">callme</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buflen</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$errmsg</span><span style="color: #007700">)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buffer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;convert&nbsp;to&nbsp;upper&nbsp;case&nbsp;and&nbsp;replace&nbsp;","&nbsp;delimiter&nbsp;with&nbsp;[TAB]<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buffer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">","</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /><br /><br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Input:\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_set_local_infile_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"callme"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"LOAD&nbsp;DATA&nbsp;LOCAL&nbsp;INFILE&nbsp;'input.txt'&nbsp;INTO&nbsp;TABLE&nbsp;t1"</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_set_local_infile_default</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;t1"</span><span style="color: #007700">);<br /><br /><br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\nResult:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">join</span><span style="color: #007700">(</span><span style="color: #DD0000">","</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Input:
23,foo
42,bar

Output:
23,FOO
42,BAR
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mysqli.set-local-infile-handler-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli.set-local-infile-default.html" class="function" rel="rdfs-seeAlso">mysqli_set_local_infile_default()</a> - Unsets user defined handler for load local infile command</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.set-local-infile-default.html">mysqli::set_local_infile_default</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.sqlstate.html">mysqli::$sqlstate</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
